package queue;

public class MyLinkQueue {
    static class ListNode{
        public int val;
        public ListNode pre;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode tail;

    public int usedSize;

    public void offer(int val){
        ListNode node = new ListNode(val);
        if (head == null){
            head = node;
            tail = node;
        }else{
            tail.next = node;
            node.pre = tail;
            tail = tail.next;
        }
        usedSize++;
    }

    public int poll(){
        if (head == null){
            return -1;
        }
        int ret = head.val;
        if(head.next == null){
            head = null;
            tail = null;
            usedSize--;
            return ret;
        }
            head = head.next;
            head.pre = null;
            usedSize--;
            return ret;
    }

    public int peek(){
        if (head == null){
            return -1;
        }
        return head.val;
    }

    public boolean empty(){
        return head == null;
    }

    public int size(){
        return usedSize;
    }
}
